﻿using System;
using System.Windows.Navigation;

namespace SL4_MVVM_MEF_Navigation
{
    /// <summary>
    /// Custom Navigation Content Loader
    /// </summary>
    public class CustomContentLoader : INavigationContentLoader
    {
        PageResourceContentLoader _contentLoader = new PageResourceContentLoader();

        public IAsyncResult BeginLoad(Uri targetUri, Uri currentUri, AsyncCallback userCallback, object asyncState)
        {
            if (targetUri == new Uri("/Views/WCFRIAModule.xaml", UriKind.RelativeOrAbsolute)
                || (targetUri == new Uri("/Views/MEFModule.xaml", UriKind.RelativeOrAbsolute)))
            {
                return _contentLoader.BeginLoad(new Uri("/Views/MEFModuleContainer.xaml", UriKind.RelativeOrAbsolute),
                    currentUri, userCallback, asyncState);
            }
            else return _contentLoader.BeginLoad(targetUri, currentUri, userCallback, asyncState);
        }

        public bool CanLoad(Uri targetUri, Uri currentUri)
        {
            if (targetUri == new Uri("/Views/WCFRIAModule.xaml", UriKind.RelativeOrAbsolute)
                || (targetUri == new Uri("/Views/MEFModule.xaml", UriKind.RelativeOrAbsolute)))
            {
                return _contentLoader.CanLoad(new Uri("/Views/MEFModuleContainer.xaml", UriKind.RelativeOrAbsolute), currentUri);
            }
            else return _contentLoader.CanLoad(targetUri, currentUri);
        }

        public void CancelLoad(IAsyncResult asyncResult)
        {
            _contentLoader.CancelLoad(asyncResult);
        }

        public LoadResult EndLoad(IAsyncResult asyncResult)
        {
            return _contentLoader.EndLoad(asyncResult);
        }
    }
}
